﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VASJ.Model;
using System.Web.Security;
using VASJ.Service.Library;
using VASJ.Service.Library.Manager;

namespace VASJ.Service
{
    public partial class AcpInetService
    {
        #region "AHSPermission"
        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Set permission for user in project
        /// </summary>
        /// <param name="applicationName">Application name of project</param>
        /// <param name="userName"></param>
        /// <param name="projectId"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool SetPermissionForUser(string applicationName, string userName, int projectId, string permissionName)
        {
            bool retVal = true;

            try
            {
                Common cm = new Common(applicationName);
                PermissionTypeManager ptm = new PermissionTypeManager(applicationName);

                if (cm.ValidateProject(projectId))
                {
                    //Get PermissionID
                    int permissionID = cm.GetPermissionID(permissionName);
                    Guid userID = cm.GetUserID(userName);

                    // Check user 
                    if (!cm.IsExistPermissionForUser(userID, permissionID, projectId))
                    {
                        ptm.AddPermissionForUser(userID, permissionID, projectId);
                    }

                    //Check user in project
                    if (!cm.IsUserInProject(userName, projectId))
                    {
                        cm.AddUserToProject(userName, projectId);
                    }
                }
            }
            catch (Exception ex)
            {
                retVal = false;

                Logger.Log.ErrorFormat("[" + applicationName + "] Permission.SetPermissionForUser error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// SetPermissionsForUser
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="userName"></param>
        /// <param name="projectId"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool SetPermissionsForUser(string applicationName, string userName, int projectId, string[] permissionName)
        {
            bool retVal = true;

            try
            {
                Common cm = new Common(applicationName);
                PermissionTypeManager ptm = new PermissionTypeManager(applicationName);
                Guid userID = cm.GetUserID(userName);

                if (cm.ValidateProject(projectId))
                {
                    for (int i = 0; i < permissionName.Length; i++)
                    {
                        //Get PermissionID
                        int permissionID = cm.GetPermissionID(permissionName[i]);

                        if (!cm.IsExistPermissionForUser(userID, permissionID, projectId))
                        {
                            ptm.AddPermissionForUser(userID, permissionID, projectId);

                            //Check user in project
                            if (!cm.IsUserInProject(userName, projectId))
                            {
                                cm.AddUserToProject(userName, projectId);
                            }
                        }
                    }

                    this.mdb.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                retVal = false;

                Logger.Log.ErrorFormat("[" + applicationName + "] Permission.SetPermissionsForUser error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// SynchPermissionType
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool SynchPermissionType(string ApplicationName, string[] permissionName)
        {
            bool retVal = true;

            try
            {
                //Get All Permision for Application
                List<string> lst_pers = (new Common(ApplicationName)).GetAllPermission();
                PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

                for (int i = 0; i < permissionName.Length; i++)
                {
                    if (!lst_pers.Contains(permissionName[i]))
                    {
                        retVal = ptm.CreatePermission(permissionName[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                retVal = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.SynchPermissionType error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GrantPermissionForRole
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="roleName"></param>
        /// <param name="permissionName"></param>
        /// <param name="grant"></param>
        /// <returns></returns>
        public bool GrantPermissionForRole(string ApplicationName, string roleName, string permissionName, bool grant)
        {
            bool retVal = true;

            try
            {
                //Get roleID
                Guid roleID = (new Common(ApplicationName)).GetRoleID(roleName);

                //Get permissionID
                int permissionID = (new Common(ApplicationName)).GetPermissionID(permissionName);

                if (roleID != Guid.Empty && permissionID != 0)
                {
                    retVal = new RoleManager(ApplicationName).GrantPermission(roleID, permissionID, grant);
                }
            }
            catch (Exception ex)
            {
                retVal = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GrantPermissionForRole error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// RemovePermissionForTarget
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <param name="projectID"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool RemovePermissionForTarget(string ApplicationName, string userName, int projectID, string permissionName)
        {
            try
            {
                //Get UserID
                MembershipUser mu = Membership.GetUser(userName);
                Guid userID = Guid.Parse(mu.ProviderUserKey.ToString());

                //Get permissionID
                int permissionID = (new Common(ApplicationName)).GetPermissionID(permissionName);

                return new PermissionTypeManager(ApplicationName).RemovePermissionForTarget(userID, projectID, permissionID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.RemovePermissionForTarget error {0}", ex);

                return false;
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// RemovePermissionsForTarget
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <param name="projectID"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool RemovePermissionsForTarget(string ApplicationName, string userName, int projectID, string[] permissionName)
        {
            try
            {
                // Get UserID
                MembershipUser mu = Membership.GetUser(userName);
                Guid userID = Guid.Parse(mu.ProviderUserKey.ToString());

                PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

                for (int i = 0; i < permissionName.Length; i++)
                {
                    // Get permissionID
                    int permissionID = (new Common(ApplicationName)).GetPermissionID(permissionName[i]);

                    ptm.RemovePermissionForTarget(userID, projectID, permissionID);
                }

                return true;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.RemovePermissionsForTarget error {0}", ex);

                return false;
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// RemovePermissionForUser
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool RemovePermissionForUser(string ApplicationName, string userName)
        {
            try
            {
                // Get UserID
                MembershipUser mu = Membership.GetUser(userName);
                Guid userID = Guid.Parse(mu.ProviderUserKey.ToString());

                PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

                return ptm.RemovePermissionForUser(userID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.RemovePermissionForUser error {0}", ex);

                return false;
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// RemovePermissionForRole
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public bool RemovePermissionForRole(string ApplicationName, string roleName)
        {
            try
            {
                // Get roleID
                Guid roleID = (new Common(ApplicationName)).GetRoleID(roleName);
                PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

                if (roleID != Guid.Empty)
                {
                    return ptm.RemovePermissionForRole(roleID);
                }

                return false;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.RemovePermissionForRole error {0}", ex);

                return false;
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// IsGrantedUser
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <param name="projectID"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool IsGrantedUser(string ApplicationName, string userName, int projectID, string permissionName)
        {
            bool retVal = false;

            try
            {
                Common cm = new Common(ApplicationName);

                if (cm.ValidateProject(projectID))
                {
                    int permissionID = cm.GetPermissionID(permissionName);
                    Guid userID = cm.GetUserID(userName);

                    return new PermissionTypeManager(ApplicationName).IsExistPermissionForUser(userID, permissionID, projectID);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.IsGrantedUser error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// IsGrantedUser2
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <param name="projectID"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool IsGrantedUser2(string ApplicationName, string userName, int projectID, string[] permissionName)
        {
            bool retVal = false;

            try
            {
                Common cm = new Common(ApplicationName);
                Guid userID = cm.GetUserID(userName);
                PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

                if (cm.ValidateProject(projectID))
                {
                    for (int i = 0; i < permissionName.Length; i++)
                    {
                        int permissionID = cm.GetPermissionID(permissionName[i]);

                        retVal = ptm.IsExistPermissionForUser(userID, permissionID, projectID);

                        if (retVal) break;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.IsGrantedUser2 error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetGrantedPermission
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public List<int> GetGrantedPermission(string ApplicationName, string userName, string permissionName)
        {
            List<int> lstProject = new List<int>();

            try
            {
                Common cm = new Common(ApplicationName);
                Guid userID = cm.GetUserID(userName);
                int permissionID = cm.GetPermissionID(permissionName);

                lstProject = new PermissionTypeManager(ApplicationName).GetProjectsGrantedPermission(userID, permissionID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetGrantedPermission error {0}", ex);
            }

            return lstProject;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetGrantedPermission2
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public List<int> GetGrantedPermission2(string ApplicationName, string userName, string[] permissionName)
        {
            List<int> lstProject = new List<int>();

            try
            {
                Common cm = new Common(ApplicationName);
                Guid userID = cm.GetUserID(userName);
                PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

                for (int i = 0; i < permissionName.Length; i++)
                {
                    int permissionID = cm.GetPermissionID(permissionName[i]);

                    List<int> lstTemp = ptm.GetProjectsGrantedPermission(userID, permissionID);

                    foreach (var it in lstTemp)
                    {
                        if (!lstProject.Contains(it)) lstProject.Add(it);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetGrantedPermission2 error {0}", ex);
            }

            return lstProject;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetGratedUser
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectID"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public List<Guid> GetGratedUser(string ApplicationName, int projectID, string permissionName)
        {
            List<Guid> lstUser = new List<Guid>();
            PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

            try
            {
                Common cm = new Common(ApplicationName);

                if (cm.ValidateProject(projectID))
                {
                    int permissionID = cm.GetPermissionID(permissionName);

                    lstUser = ptm.GetGrantedUserForProject(projectID, permissionID);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetGratedUser error {0}", ex);
            }

            return lstUser;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Get user list is granted in project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectID"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public List<string> GetGratedUserName(string ApplicationName, int projectID, string permissionName)
        {
            List<string> lstUser = new List<string>();
            PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

            try
            {
                Common cm = new Common(ApplicationName);

                if (cm.ValidateProject(projectID))
                {
                    int permissionID = cm.GetPermissionID(permissionName);

                    lstUser = ptm.GetGrantedUserNameForProject(projectID, permissionID);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetGratedUser2 error {0}", ex);
            }

            return lstUser;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetGratedUser2
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectID"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public List<Guid> GetGratedUser2(string ApplicationName, int projectID, string[] permissionName)
        {
            List<Guid> lstUser = new List<Guid>();
            PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

            try
            {
                Common cm = new Common(ApplicationName);

                if (cm.ValidateProject(projectID))
                {
                    for (int i = 0; i < permissionName.Length; i++)
                    {
                        int permissionID = cm.GetPermissionID(permissionName[i]);

                        List<Guid> lstTemp = ptm.GetGrantedUserForProject(projectID, permissionID);

                        foreach (var it in lstTemp)
                        {
                            if (!lstUser.Contains(it)) lstUser.Add(it);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetGratedUser2 error {0}", ex);
            }

            return lstUser;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Get the username list of project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectID"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public List<string> GetGratedUserName2(string ApplicationName, int projectID, string[] permissionName)
        {
            List<string> lstUser = new List<string>();
            PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

            try
            {
                Common cm = new Common(ApplicationName);

                if (cm.ValidateProject(projectID))
                {
                    for (int i = 0; i < permissionName.Length; i++)
                    {
                        int permissionID = cm.GetPermissionID(permissionName[i]);

                        List<string> lstTemp = ptm.GetGrantedUserNameForProject(projectID, permissionID);

                        foreach (var user in lstTemp)
                        {
                            if (!lstUser.Contains(user)) lstUser.Add(user);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetGratedUser2 error {0}", ex);
            }

            return lstUser;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetRoleOfPermission
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public List<string> GetRoleOfPermission(string ApplicationName, string permissionName)
        {
            List<string> lstRole = new List<string>();
            PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

            try
            {
                Common cm = new Common(ApplicationName);
                int permissionID = cm.GetPermissionID(permissionName);

                lstRole = ptm.GetRoleOfPermission(permissionID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetRoleOfPermission error {0}", ex);
            }

            return lstRole;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetRoleOfPermissions
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public List<string> GetRoleOfPermissions(string ApplicationName, string[] permissionName)
        {
            List<string> lstRole = new List<string>();
            PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

            try
            {
                Common cm = new Common(ApplicationName);

                for (int i = 0; i < permissionName.Length; i++)
                {
                    int permissionID = cm.GetPermissionID(permissionName[i]);
                    List<string> lstTemp = ptm.GetRoleOfPermission(permissionID);

                    foreach (var it in lstTemp)
                    {
                        if (lstRole.Contains(it)) lstRole.Add(it);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetRoleOfPermissions error {0}", ex);
            }

            return lstRole;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// IsUserHasRolePermission
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <param name="Role"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool IsUserHasRolePermission(string ApplicationName, string userName, string[] Role, string permissionName)
        {
            bool retVal = false;

            RolePermissionManager rpm = new RolePermissionManager(ApplicationName);

            try
            {
                retVal = rpm.IsUserHasRolePermission(userName, permissionName);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.IsUserHasRolePermission error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// IsUserHasRolePermissions
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <param name="Role"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool IsUserHasRolePermissions(string ApplicationName, string userName, string[] Role, string[] permissionName)
        {
            bool retVal = false;

            RolePermissionManager rpm = new RolePermissionManager(ApplicationName);

            try
            {
                for (int j = 0; j < permissionName.Length; j++)
                {
                    retVal = rpm.IsUserHasRolePermission(userName, permissionName[j]);

                    if (retVal) break;
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.IsUserHasRolePermissions error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetRolePermission
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public List<string> GetRolePermission(string ApplicationName, string roleName)
        {
            List<string> lstPermission = new List<string>();

            try
            {
                lstPermission = new PermissionTypeManager(ApplicationName).GetPermissionListForRole(roleName);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetRolePermission error {0}", ex);
            }

            return lstPermission;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetRolesPermission
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public List<string> GetRolesPermission(string ApplicationName, string[] roleName)
        {
            List<string> lstPermission = new List<string>();

            PermissionTypeManager ptm = new PermissionTypeManager(ApplicationName);

            try
            {
                for (int i = 0; i < roleName.Length; i++)
                {
                    List<string> lstTemp = ptm.GetPermissionListForRole(roleName[i]);

                    foreach (var it in lstTemp)
                    {
                        if (!lstPermission.Contains(it)) lstPermission.Add(it);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetRolesPermission error {0}", ex);
            }

            return lstPermission;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Get role permission for user
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public List<string> GetRolePermissionsForUser(string ApplicationName, string userName)
        {
            List<string> lstPermission = new List<string>();

            try
            {
                lstPermission = new PermissionTypeManager(ApplicationName).GetRolePermissionForUser(userName);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Permission.GetRolePermissionsForUser error {0}", ex);
            }

            return lstPermission;
        }
        #endregion
    }
}